package cn.vin.thread;

/**
 * @Author: Wu Shenzhuang
 * @Date: 2019/5/23 10:46
 */

public class ShutdownHookDemo extends Thread {
    private Thread mainThread;
    @Override
    public void run() {
        System.out.println("========shutdown");
//        mainThread.interrupt();
        try {
            mainThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("========system exit");
    }

    public ShutdownHookDemo(Thread mainThread) {
        Runtime.getRuntime().addShutdownHook(this);
        this.mainThread = mainThread;
    }

    public static void main(String[] args) throws InterruptedException {
        ShutdownHookDemo shutdownHookDemo = new ShutdownHookDemo(Thread.currentThread());
        System.out.println("==========main start");
        Thread.sleep(1000 * 3);
        System.out.println("==========main end");
    }
}
